import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/Login.vue'
import {checktoken} from '../apis/user'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: login
  },
  {
    path: '/index',
    name: 'index',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import('../views/Inedex.vue'),
    children:[{
      path: '/',
      name: 'Backstage',
      component: () => import('../views/modeules/Backstage.vue'),
    },{
      path: '/index/Order',
      name: 'Order',
      component: () => import('../views/modeules/Order.vue'),
    },{
      path:'/index/Aaccount',
      name: 'Aaccount',
      component: () => import('../views/modeules/accounts/Aaccount .vue'),
    },{
      path:'/index/Addaccount',
      name: 'Addaccount',
      component: () => import('../views/modeules/accounts/Addaccount.vue'),
    },{
      path:'/index/Amendpsa',
      name: 'Amendpsa',
      component: () => import('../views/modeules/accounts/Amendpsa.vue'),
    },{
      path:'/index/Shop',
      name: 'Shop',
      component: () => import('../views/modeules/Shop.vue'),
    },{
      path:'/index/Productaddi',
      name: 'Productaddi',
      component: () => import('../views/modeules/Productaddi.vue'),
    },{
      path:'/index/Productf',
      name: 'Productf',
      component: () => import('../views/modeules/Productf.vue'),
    },{
      path:'/index/Productlist',
      name: 'Productlist',
      component: () => import('../views/modeules/Productlist.vue'),
    },{
      path:'/index/Statistics',
      name: 'Statistics',
      component: () => import('../views/modeules/statistics/Statistics.vue'),
    },{
      path:'/index/Uersphonto',
      name: 'Uersphonto',
      component: () => import('../views/modeules/Uersphonto.vue'),
    },{
      path:'/index/IndentST',
      name: 'IndentST',
      component: () => import('../views/modeules/statistics/IndentST.vue'),
    }]
  },{
    path:'*',
    name: '404',
    component: () => import('../views/modeules/err404.vue'),
  }
]


const router = new VueRouter({
  routes
})
// 路由全局守卫
// 全局路由跳转之前 to是到哪里 next函数是放行去哪里
router.beforeEach((to,from,next)=>{
  if(to.path=='/'){
    next()
  }else{
    checktoken({ token:localStorage.token}).then((res)=>{
      if(res.data.code==0){
        next()
      }else{
        next('/')
      }
    })
  }
})
export default router
